package com.taplinker.client.core;

import java.io.File;
import java.io.IOException;

import android.os.Environment;

import com.taplinker.client.AppConfigFactory;
import com.taplinker.core.Resource;
import com.taplinker.core.ResourceManager;
import com.taplinker.core.util.DeviceUtil;

public final class LocalFileManager implements Resource {
	private static final String APP_CAMERA_DIR = "/camera";
	private static final String APP_LOG_DIR = "/log";
	private static final String USER_IMAGE_DIR = "/image";
	private static final String USER_IMAGE2_DIR = "/image2";
	private static final String USER_VOICE_DIR = "/voice";
	private static final String USER_VIDEO_DIR = "/video";
	
	private static String APP_ROOT_DIR = AppConfigFactory.getInstance().getApplication().getExternalCacheDir().getParent();
	private static String USER_DIR = "/" + AppConfigFactory.getInstance().getUserInfo().getUserid();
	
	private static LocalFileManager instance;
	
	private LocalFileManager() {
	}

	public static LocalFileManager getInstance() {
		if(instance != null) return instance;
		synchronized (LocalFileManager.class) {
			if(instance == null) {
				instance = new LocalFileManager();
				ResourceManager.getInstance().addResource(instance);
			}
		}
		return instance;
	}
	
	public String getAppRootDir() {
		if(DeviceUtil.hasSdCard()) {
			return Environment.getExternalStorageDirectory().getPath() + APP_ROOT_DIR;
		} else {
			return AppConfigFactory.getInstance().getApplication().getFilesDir().getPath();
		}
	}
	
	public String getAppUserRootDir() {
		return getAppRootDir() + "/" + AppConfigFactory.getInstance().getUserInfo().getUserid();
	}
	
	public void ayscCreateDirs() {
		new Thread() {
			
			@Override
			public void run() {
				super.run();
				createDirs();
			}
		}.start();
		
	}
	
	public void createDirs() {
		File file1 = new File(getAppRootDir());
		if (!file1.exists()) {
			file1.mkdirs();
		}
		File file2 = new File(getAppLogDir());
		if (!file2.exists()) {
			file2.mkdirs();
		}
		File file3 = new File(getCameraDir());
		if (!file3.exists()) {
			file3.mkdirs();
		}
		File file4 = new File(getImageDir());
		if (!file4.exists()) {
			file4.mkdirs();
		}
		File file5 = new File(getImage2Dir());
		if (!file5.exists()) {
			file5.mkdirs();
		}
		File file6 = new File(getVoiceDir());
		if (!file6.exists()) {
			file6.mkdirs();
		}
		File file7 = new File(getVideoDir());
		if (!file7.exists()) {
			file7.mkdirs();
		}
		File file8 = new File(getVideoDir());
		if (!file8.exists()) {
			file8.mkdirs();
		}
		try {
			createNomediaFiles();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void createNomediaFiles() throws IOException {
		File file1 = new File(getAppLogDir() + "/.nomedia");
		if(!file1.exists()) {
			file1.createNewFile();
		}
		File file3 = new File(getImageDir() + "/.nomedia");
		if(!file3.exists()) {
			file3.createNewFile();
		}
		File file4 = new File(getImage2Dir() + "/.nomedia");
		if(!file4.exists()) {
			file4.createNewFile();
		}
		File file5 = new File(getVoiceDir() + "/.nomedia");
		if(!file5.exists()) {
			file5.createNewFile();
		}
		File file6 = new File(getVideoDir() + "/.nomedia");
		if(!file6.exists()) {
			file6.createNewFile();
		}
	}
	
	public String getAppLogDir() {
		return getAppRootDir() + APP_LOG_DIR;
	}
	
	public String getCameraDir() {
		return getAppRootDir() + APP_CAMERA_DIR;
	}
	
	/**
	 * 文本缓存
	 * @return
	 */
	public String getCacheDir() {
		if(DeviceUtil.hasSdCard()) {
			return AppConfigFactory.getInstance().getApplication().getExternalCacheDir().getPath();
		} else {
			return AppConfigFactory.getInstance().getApplication().getCacheDir().getPath();
		}
	}
	
	/**
	 * 压缩大图、各类原图
	 * @return
	 */
	public String getImageDir() {
		return getAppRootDir() + USER_DIR + USER_IMAGE_DIR;
	}
	
	/**
	 * 压缩小图
	 * @return
	 */
	public String getImage2Dir() {
		return getAppRootDir() + USER_DIR + USER_IMAGE2_DIR;
	}
	
	public String getVoiceDir() {
		return getAppRootDir() + USER_DIR + USER_VOICE_DIR;
	}
	
	public String getVideoDir() {
		return getAppRootDir() + USER_DIR + USER_VIDEO_DIR;
	}

	@Override
	public void clear() {
		
	}

	@Override
	public void releaseResource() {
		
	}
	
}
